#include <stdio.h>
#include <string.h>

int maxLen01(char *s){
	if(*s == '\0'){
		return 0;
	}
	char *read = s;
	short len = 1, max_len = 1;
	while(*(++read) != '\0'){
		char isDiff = 1;
		char *font = read - 1;
		short i;
		for(i = 0; i < len; i++, font--){
			if(*font == *read){
				isDiff = 0;
				i++;
				break;
			}
		}
		if(isDiff){
			len++;
		} else {
			max_len = (len > max_len) ? len : max_len;
			len = i;
		}
	}
	max_len = (len > max_len) ? len : max_len;
	return max_len;
}


int maxLen02(char *s){
	if(*s == '\0'){
		return 0;
	}
	short read = 0;
	short head = 0;
	short max_len = 1;
	while(s[++read] != '\0'){
		char isDiff = 0;
		short font = read;
		for(; --font >= head;){
			if(s[font] == s[read]){
				isDiff = 1;
				break;
			}
		}
		if(isDiff){
			char len = read - head;
			max_len = (len > max_len) ? len : max_len;
			head = font + 1;
		}
	}
	short len = read - head;
	max_len = (len > max_len) ? len : max_len;
	return max_len;
}


int maxLen03(char *s){
	int let[128]; 
	memset(let, -1, sizeof(let));
	char *read = s;  
	int max_len = 0, len = 0, index = 0;  
	while (*read != '\0') {  
		if (let[*read] == -1 || index - let[*read] > len) {
			len++;
		} else {
			max_len = (len > max_len) ? len : max_len;
			len = index - let[*read]; 
		}  
		let[*read] = index;
		read++;  
		index++;  
	} 
	max_len = (len > max_len) ? len : max_len;  
	return max_len;
}


int maxLen04(char *s){
	int let[128];
	memset(let, -1, sizeof(let));
	char *read = s;
	int max_len = 0;
	int index = 0;
	int len = 0;
	while(*read != 0){
		if(index - len > let[*read]){
			len++;
		} else {
			max_len = (len > max_len) ? len : max_len;
			len = index - let[*read];
		}
		let[*read] = index;
		read++;
		index++;
	}
	return (len > max_len) ? len : max_len;
}


int main(){
	char *s = "abcabcbb";
	printf("%d", maxLen03(s));
}
